package sunwin.yois.web.common.resolverArg;

import jodd.datetime.JDateTime;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by mt on 2015/8/1.
 */
public class ResolverDateArg implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterAnnotation(YoisDateParam.class) != null;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        String paraName = methodParameter.getParameterName();
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);

        String paraValue = request.getParameter(paraName);
        JDateTime jDateTime = null;

        if (paraValue != null) {
            try {
                jDateTime = new JDateTime(paraValue);
                return jDateTime;
            } catch (Exception ex) {
            }
        }
        return jDateTime;
    }
}
